﻿using FluentAPI.Entity.ComplexRelationship;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static FluentAPI.Entity.ComplexRelationship.Order;

namespace FluentAPI.Entity
{
    class BlogEFContext:DbContext
    {
        //  public BlogEFContext():base("name="ConnectionString") 在webconfig 或者App.conig中配置链接字符串
        public BlogEFContext()
        {
            //如果数据不存在 就创建
            // Database.SetInitializer(new CreateDatabaseIfNotExists<BlogEFContext>());

            //总是创建数据库,无论是否存在
            //Database.SetInitializer(new  DropCreateDatabaseAlways<BlogEFContext>());


            //如果检测到ef模型发了变化，讲更新模型
            Database.SetInitializer (new DropCreateDatabaseIfModelChanges<BlogEFContext>());

            //也可以在配置文件AppSeting节点中配置

        }

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Order> Order { get; set; }

        public DbSet<Customer> Customer { get; set; }
        public DbSet<Student> Student { get; set; }
        public DbSet<Course> Course { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //复杂类型约定 将Adress的字段作为Oder 属性的不一部分
            // modelBuilder.Entity<Order>().ToTable("Orders");
            //modelBuilder.ComplexType<Address>();

            // 全局约定
            // 只要模型属性中有属性ID 就讲ID 作为主键
            // modelBuilder.Properties().Where(p => p.Name == "ID").Configure(p => p.IsKey());

            //base.OnModelCreating(modelBuilder);

            //自定义约定
            // modelBuilder.Convention.Add<CustomKeyConvention>();
            //modelBuilder.Conventions.Add<CustomKeyConvention>();

            //自定义 对所有模型中属性为decimal的类型 配置位数10 且保留两个小数点
            // modelBuilder.Properties<decimal>().Configure(config => config.HasPrecision(10, 2));

            //多个相同的配置 最后一个约定配置将覆盖前面相同的配置  最后的配置为250
            //modelBuilder.Properties<string>().Configure(c => c.HasMaxLength(500));
            //modelBuilder.Properties<string>().Configure(c => c.HasMaxLength(250));


            //将映射类配置到Config中
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type => !string.IsNullOrEmpty(type.Namespace))
                .Where(type => type.BaseType != null && type.BaseType.IsGenericType
                     && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }
            base.OnModelCreating(modelBuilder);




        }
        /// <summary>
        /// 自定义全局配置
        /// </summary>
        public class CustomKeyConvention : Convention
        {
            public CustomKeyConvention()
            {
                Properties().Where(prop => prop.Name == "ID").Configure(prop => prop.IsKey());
            }

        }




    }
}
